<?php
namespace app\api\controller;

use think\Cache;
use think\Db;
use think\Image;
use app\api\controller\Base;
use app\api\controller\Message;
use think\Session;
use think\Request;

class Xinxi extends Base
{


  //点赞添加的信息
  public function dianzan()
  {
    $openid = $this->uid;//openid
    $name = db('user')->where('uid',$openid)->find();
    $name['uname'] = $name['uname']==''? $name['nickname']:$name['uname'];
    $mid = input('mid');//接收文章mid
    $uflg=db('user')->where('uid',$openid)->value('uflg');
    $mes = new Message();
    if($uflg!=0)
    {
      if(!empty($mid))
      {
        $cha = db('message')->where('mid', $mid)->field('id')->find();
        if ($cha['id'] == '') {
          $str = $openid . ',';
          $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
          $time = date('Y-m-d H:i:s');
          $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
          if ($cha) {
            $abc = db('message')->where('mid', $mid)->field('id,uid,title,type,content')->find();
            if($openid != $abc['uid']){
              $abc['id']=explode (',',$abc['id']);
              $abc['id']=array_filter($abc['id']);
              $abc['id']=count($abc['id']);
              $openid=db('user')->where('uid',$abc['uid'])->field('code')->find();
              if($abc['type']==1){
                $abc['content']=substr($abc['content'],0,24).'...';
                $abc['title']=$abc['content'];
              }
              $a = $mes->dianzan($openid['code'],$name['uname'],$abc['uid'],$abc['id'],$abc['title']);
            }
            //file_put_contents('a.php',$a);
            return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
          }
        } else {
          $id = array_filter(explode(',', $cha['id']));
          if (in_array($openid, $id)) {
            return $this->AjaxReturn("-1", "该文章你已经点赞过！");
          } else {
            $str = $cha['id'] . $openid . ',';
            $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
            $time = date('Y-m-d H:i:s');
            $rs = db('praise')->insert(['mid' => $mid, 'uid' => $openid, 'createtime' => $time]);
            if ($cha) {
              $abc = db('message')->where('mid', $mid)->field('id,uid,title,type,content')->find();
              $abc['id']=explode (',',$abc['id']);
              $abc['id']=array_filter($abc['id']);
              $abc['id']=count($abc['id']);
              $openid=db('user')->where('uid',$abc['uid'])->field('code')->find();
              if($abc['type']==1){
                $abc['content']=substr($abc['content'],0,24).'...';
                $abc['title']=$abc['content'];
              }
              $a = $mes->dianzan($openid['code'],$name['uname'],$abc['uid'],$abc['id'],$abc['title']);
              return $this->AjaxReturn("1", "点赞成功！",$name['uname']);
            }
          }
        }
      }
      else
      {
        return $this->AjaxReturn("-1","未接收到文章mid！");
      }
    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权操作！");
    }

  }

  //取消点赞的操作
  public function quxiao()
  {
    $openid = $this->uid;//openid
    $name = db('user')->where('uid',$openid)->find();
    $name['uname'] = $name['uname']==''? $name['nickname']:$name['uname'];
    $mid = input('mid');//接收文章mid
    $uflg=db('user')->where('uid',$openid)->value('uflg');
    if($uflg!=0)
    {
      if(!empty($mid))
      {
        $cha = db('message')->where('mid', $mid)->field('id')->find();
        $id = array_filter(explode(',', $cha['id']));
        if (!in_array($openid, $id)) {
          return $this->AjaxReturn("-1", "该文章你未点过赞！");
        } else {
          foreach ($id as $key=>$value)
          {
            if($openid == $value)
            {
              unset($id[$key]);
            }
          }
          //最后一个取消关注的要删除逗号
          if(count($id)=='')
          {
            //return 1;die;
            $str=implode(',',$id);
          }
          else
          {
            //return 2;die;
            $str=implode(',',$id).',';
          }
          $cha = db('message')->where('mid', $mid)->update(['id' => $str]);
          $rs = db('praise')->where('mid',$mid)->where('uid',$openid)->delete();
          if ($cha && $rs) {
            return $this->AjaxReturn("1", "取消点赞成功！",$name['uname']);
          }
        }
      }
      else
      {
        return $this->AjaxReturn("-1","未接收到文章mid！");
      }
    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权操作！");
    }

  }

  //一级评论的添加
  public function pinglunad()
  {
    $openid = $this->uid;//openid
    $mid = input('mid');//接收文章mid
    $content=input('content');//评价内容
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c1a86ee3e43e4a3&secret=32f54b415ab46d898b2714c327b8d4b9";
    // 创建一个新cURL资源
    $ch  =  curl_init ();
    // 设置URL和相应的选项
    curl_setopt ( $ch , CURLOPT_URL ,  $url );
    curl_setopt ( $ch , CURLOPT_HEADER ,  false );
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch , CURLOPT_SSLVERSION, 1);
    // 抓取URL并把它传递给浏览器
    $str = curl_exec ( $ch );
    //关闭cURL资源，并且释放系统资源
    curl_close ( $ch );
    //将str->数组
    $arr = json_decode($str , true);
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$arr['access_token'];
    $content=$content;
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容，最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,true);
    if($output['errmsg']!="ok")
    {
    	return $this->AjaxReturn('0','您的评论里含有敏感词汇，请删除后重新发布');
    }
    //$formid = input('formId');//formid
    $user = db('message')->where('mid',$mid)->field('uid')->find();
    $use = db('user')->where('uid',$user['uid'])->field('code')->find();
    $uses = db('user')->where('uid',$openid)->field('uname,phone,nickname')->find();
    if ($uses['uname']==''){
      $name = $uses['nickname'];
    }else{
      $name = $uses['uname'];

    }
    $mes = new Message();

    $uflg=db('user')->where('uid',$openid)->value('uflg');
    if($uflg!=0)
    {


      if(!empty($mid))
      {
        $time=date('Y-m-d H:i:s');
        $add=db('evalute')->insert(['message_id'=>$mid,'pjid'=>$openid,'bpid'=>0,'parent_evalute_id'=>0,'create_time'=>$time,'txt'=>$content,'sfig'=>1]);

        if ($add) {
          if($openid != $user['uid']){
            $res= $mes->index($use['code'],$name,$uses['phone'],$content,$user['uid']);
          }
          //file_put_contents('a.php',$res);die;
          return $this->AjaxReturn("1", "评论成功！");
        }
      }
      else
      {
        return $this->AjaxReturn("-1","未接收到文章mid！");
      }


    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权评论！");
    }


  }

  //二级评论的添加
  public function pinglunadd()
  {
    $openid = $this->uid;//openid
    $mid = input('mid');//接收文章mid
    $content=input('content');//评价内容
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6c1a86ee3e43e4a3&secret=32f54b415ab46d898b2714c327b8d4b9";
    // 创建一个新cURL资源
    $ch  =  curl_init ();
    // 设置URL和相应的选项
    curl_setopt ( $ch , CURLOPT_URL ,  $url );
    curl_setopt ( $ch , CURLOPT_HEADER ,  false );
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch , CURLOPT_SSLVERSION, 1);
    // 抓取URL并把它传递给浏览器
    $str = curl_exec ( $ch );
    //关闭cURL资源，并且释放系统资源
    curl_close ( $ch );
    //将str->数组
    $arr = json_decode($str , true);
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$arr['access_token'];
    $content=$content;
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容，最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,true);
    if($output['errmsg']!="ok")
    {
    	return $this->AjaxReturn('0','您的评论里含有敏感词汇，请删除后重新发布');
    }
    $uflg=db('user')->where('uid',$openid)->value('uflg');
    $user = db('message')->where('mid',$mid)->field('uid')->find();
    $mes = new Message();
    if($uflg!=0)
    {
      //去掉表情
      if($content=='')
      {
        return $this->AjaxReturn("-2", "评论内容不支持表情！");
      }
      else
      {
        if(!empty($mid))
        {
          $evalute_id=input('evalute_id');//id上级评论
          $pjid=db('evalute')->where('evalute_id',$evalute_id)->field('pjid')->find();
          $time=date('Y-m-d H:i:s');
          $add=db('evalute')->insert(['message_id'=>$mid,'pjid'=>$openid,'bpid'=>$pjid['pjid'],'parent_evalute_id'=>$evalute_id,'create_time'=>$time,'txt'=>$content,'sfig'=>1]);
          if($openid != $pjid['pjid']){

            $e=db('user')->where('uid',$openid)->field('uname,nickname,phone')->find();
            $bbb=db('user')->where('uid',$pjid['pjid'])->field('code')->find();

            if($e['uname']=='')
            {
              $e['uname']=$e['nickname'];
            }
            $res= $mes->index($bbb['code'],$e['uname'],$e['phone'],$content,$pjid['pjid']);//上级id的openid，我的name，我的phone，回复内容，上级id的uid
          }
          if ($add) {
            return $this->AjaxReturn("1", "评论成功！");
          }
        }
        else
        {
          return $this->AjaxReturn("-1","未接收到文章mid！");
        }
      }

    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权评论！");
    }

  }

  //拨号的添加
  public function bohao()
  {
    $openid = $this->uid;//openid
    $mid = input('mid');//接收文章mid
    $uflg=db('user')->where('uid',$openid)->value('uflg');
    if($uflg!=0)
    {
      if(!empty($mid))
      {
        $bjid=db('message')->where('mid',$mid)->field('uid')->find();
        //判断是否认证&是否是官方账号
        $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
        //查询用户或者会员拨号设置的次数
        $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao')->find();
        $timestart=date("Y-m-d 00:00:00");
        $timeend=date("Y-m-d H:i:s");
        $shu=db('bohao')
          ->where('zjid',$openid)
          ->where('f_bohao.b_createtime','between',[$timestart,$timeend])
          ->count();//今日拨号
        if($rflg['gflg']==1)
        {
          $time=date('Y-m-d H:i:s');
          $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
          if ($add) {
            return $this->AjaxReturn("1", "拨号成功！");
          }
        }
        else
        {
          if($rflg['rflg']==1)
          {
            if($num['vipbohao']>$shu)
            {
              $time=date('Y-m-d H:i:s');
              $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
              if ($add) {
                return $this->AjaxReturn("1", "拨号成功！");
              }
            }
            else
            {
              return $this->AjaxReturn("-4","你今日限拨次数已用完！");
            }
          }
          else
          {
            if($num['userbohao']>$shu)
            {
              $time=date('Y-m-d H:i:s');
              $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$bjid['uid'],'b_createtime'=>$time]);
              if ($add) {
                return $this->AjaxReturn("1", "拨号成功！");
              }
            }
            else
            {
              return $this->AjaxReturn("-3","你今日限拨次数已用完！");
            }
          }
        }


      }
      else
      {
        return $this->AjaxReturn("-1","未接收到文章mid！");
      }
    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权拨号！");
    }

  }

  //用户主页拨号
  public function user_bohao()
  {
    $openid = $this->uid;//openid
    $uid = input('uid');//接收被转发人uid
    $uflg=db('user')->where('uid',$openid)->value('uflg');
    if($uflg!=0)
    {
      if(!empty($uid))
      {
        //判断是否认证&是否是官方账号
        $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
        //查询用户或者会员拨号设置的次数
        $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao')->find();
        $timestart=date("Y-m-d 00:00:00");
        $timeend=date("Y-m-d H:i:s");
        $shu=db('bohao')
          ->where('zjid',$openid)
          ->where('f_bohao.b_createtime','between',[$timestart,$timeend])
          ->count();//今日拨号
        if($rflg['gflg']==1)
        {
          $time=date('Y-m-d H:i:s');
          $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
          if ($add) {
            return $this->AjaxReturn("1", "拨号成功！");
          }
        }
        else
        {
          if($rflg['rflg']==1)
          {
            if($num['vipbohao']>$shu)
            {
              $time=date('Y-m-d H:i:s');
              $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
              if ($add) {
                return $this->AjaxReturn("1", "拨号成功！");
              }
            }
            else
            {
              return $this->AjaxReturn("-4","你今日限拨次数已用完！");
            }
          }
          else
          {
            if($num['userbohao']>$shu)
            {
              $time=date('Y-m-d H:i:s');
              $add=db('bohao')->insert(['zjid'=>$openid,'bjid'=>$uid,'b_createtime'=>$time]);
              if ($add) {
                return $this->AjaxReturn("1", "拨号成功！");
              }
            }
            else
            {
              return $this->AjaxReturn("-3","你今日限拨次数已用完！");
            }
          }
        }


      }
      else
      {
        return $this->AjaxReturn("-1","未接收到被转发人uid！");
      }
    }
    else
    {
      return $this->AjaxReturn("-10","黑名单状态无权拨号！");
    }


  }

  //用户主页分享名片
  public function user_share()
  {
    $openid = $this->uid;//openid
    $uid = input('uid');//接收uid
    $time=date('Y-m-d H:i:s');
    $add=db('share')->insert(['zid'=>$openid,'bid'=>$uid,'createtime'=>$time]);
    if ($add) {
      return $this->AjaxReturn("1", "分享成功！");
    }
  }

  //转发的添加
  public function zhuanfa()
  {
    $openid = $this->uid;//openid
    $mid = input('mid');//接收文章mid
    if(!empty($mid))
    {
      $bjid=db('message')->where('mid',$mid)->field('f_message.uid')->find();
      //判断是否认证&是否是官方账号
      $rflg=db('real')->join('f_user us','us.uid=f_real.uid')->where('f_real.uid',$openid)->field('f_real.rflg,us.gflg')->find();
      //查询用户或者会员拨号设置的次数
      $num=db('shezhi')->where('zid',1)->field('userwenzhang,vipwenzhang,userbohao,vipbohao,userzhuanfa,vipzhuanfa')->find();
      $timestart=date("Y-m-d 00:00:00");
      $timeend=date("Y-m-d H:i:s");
      $shu=db('zhuanfa')
        ->where('zfid',$openid)
        ->where('f_zhuanfa.z_createtime','between',[$timestart,$timeend])
        ->count();//今日拨号
      //官方账号无限制
      if($rflg['gflg']==1)
      {
        $time=date('Y-m-d H:i:s');
        $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
        if ($add) {
          return $this->AjaxReturn("1", "转发成功！");
        }
      }
      else
      {
        if($rflg['rflg']==1)
        {
          if($num['vipzhuanfa']>$shu)
          {
            $time=date('Y-m-d H:i:s');
            $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
            if ($add) {
              return $this->AjaxReturn("1", "转发成功！");
            }
          }
          else
          {
            return $this->AjaxReturn("-3","你今日限转发次数已用完！");
          }
        }
        else
        {
          if($num['userzhuanfa']>$shu)
          {
            $time=date('Y-m-d H:i:s');
            $add=db('zhuanfa')->insert(['zfid'=>$openid,'fbid'=>$bjid['uid'],'z_createtime'=>$time,'mid'=>$mid]);
            if ($add) {
              return $this->AjaxReturn("1", "转发成功！");
            }
          }
          else
          {
            return $this->AjaxReturn("-3","你今日限转发次数已用完！");
          }
        }
      }
    }
    else
    {
      return $this->AjaxReturn("-1","未接收到文章mid！");
    }

  }

}
